package com.zmn.plat.admin.controller.service.tariff.citygroup;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.BaseAreaConsts;
import com.zmn.plat.model.entity.area.AreaQuery;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.entity.service.tariff.citygroup.ServiceTariffCityGroup;
import com.zmn.plat.model.entity.service.tariff.citygroup.ServiceTariffCityGroupQuery;
import com.zmn.plat.model.vo.service.tariff.citygroup.ServiceTariffCityGroupVO;
import com.zmn.plat.model.vo.service.tariff.citygroup.item.ServiceTariffCityGroupItemBindVO;

/**
 * 城市分组
 *
 * @author tanbiao
 * @since 2019-11-21 18:57:24
 */
@RequestMapping("/service/tariff/citygroup/")
@Controller
public class ServiceTariffCityGroupListController extends ServiceTariffCityGroupBaseController {
    /**
     * 列表
     *
     * @param query
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("list")
    @RequiresPermissions("/service/tariff/citygroup/list.action")
    public ModelAndView list(@ModelAttribute ServiceTariffCityGroupQuery query) {
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        BaseCategory baseCategory = baseCategoryService.findByKey(query.getCategOneId());
        ServCategory servCategory = servCategoryService.findByKey(query.getServCategId());
        // 城市需要对明细进行合并
        List<ServiceTariffCityGroupVO> groupList = faultCityGroupBService.listPageQuery(query);
        // 加载所有城市
        AreaQuery areaQuery = new AreaQuery();
        areaQuery.setLevel(BaseAreaConsts.AREA_LEVEL_THREE);
        List<BaseArea> list = baseAreaService.listByQuery(areaQuery);
        ModelAndView mav = new ModelAndView("service/tariff/citygroup/list");
        mav.addObject("items", groupList);
        mav.addObject("vo", query);
        mav.addObject("citys", list);
        mav.addObject("faultTariff", faultTariff);
        mav.addObject("servCategory", servCategory);
        mav.addObject("baseCategory", baseCategory);
        return mav;
    }

    /**
     * 详情
     *
     * @param query
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping("detail")
    @RequiresPermissions("/service/tariff/citygroup/detail.action")
    public ModelAndView detail(@ModelAttribute ServiceTariffCityGroupQuery query) {
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        BaseCategory baseCategory = baseCategoryService.findByKey(query.getCategOneId());
        ServCategory servCategory = servCategoryService.findByKey(query.getServCategId());
        ServiceTariffCityGroup cityGroup = new ServiceTariffCityGroup();
        Integer groupId = query.getGroupId();
        if (NumberUtil.isPositiveInteger(groupId)) {
            cityGroup = faultCityGroupService.findByKey(groupId);
        }
        // 加载省份城市，要求城市在同一服务分类和一级分类下不重叠，只展示没有被关联的城市，省份全部被关联，省不出现
        List<ServiceTariffCityGroupItemBindVO> selectItems = faultCityGroupItemBService.listCityCanBind(query);
        ModelAndView mav = new ModelAndView("service/tariff/citygroup/detail");
        mav.addObject("group", cityGroup);
        mav.addObject("items", selectItems);
        mav.addObject("faultTariff", faultTariff);
        mav.addObject("servCategory", servCategory);
        mav.addObject("baseCategory", baseCategory);
        mav.addObject("vo", query);
        return mav;
    }

}
